<?php

class yzDatabase extends yzCore{

	protected $aProperties = array(
	   'yz.database.db'            => 'test',
	   'yz.database.user'          => 'root',
	   'yz.database.pass'          => '',
       'yz.database.log'           => true,
	   'yz.database.engine'        => 'mysql',
	   'yz.database.host'          => 'localhost',
       'yz.database.port'          => '3306',
       'yz.database.driver'        => 'pdo',
	   'yz.database.pdo.dsn'       => '%1$s:dbname=%2$s;host=%3$s;port=%4$s',
	   'yz.database.pdo.mysql.options' => array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY=>true),
	   'yz.database.pdo.fetch_mode' => PDO::FETCH_ASSOC,
       'yz.database.default.quote'       => '"',
	); // array

	public function init(){

		if ($this->getProperty('yz.database.log')){

			$this->setObject('__log','yzRegistry');

		} //if


		// loading driver
		if (class_exists($this->getProperty('yz.database.driver'))){

		  	$sTemplate = $this->getContextProperty('dsn',$this->getProperty('yz.database.driver'));

		  	$aVars = array(
		  	   $this->getProperty('yz.database.engine'),
               $this->getProperty('yz.database.db'),
               $this->getProperty('yz.database.host'),
               $this->getProperty('yz.database.port')
		  	); // array

		  	// creating dsn
		  	$sDsn = vsprintf($sTemplate,$aVars);

		  	// connecting
		  	$sUser = $this->escape($this->getProperty('yz.database.user'));

		  	$sPass = $this->escape($this->getProperty('yz.database.pass'));

		  	$aParams = $this->getContextProperty('options',$this->getProperty('yz.database.driver').'.'.$this->getProperty('yz.database.engine'));

		  	$sDriver = $this->getProperty('yz.database.driver');

		  	// creationg db object
		    $this->setObject('__db', new $sDriver($sDsn, $sUser, $sPass, $aParams));

		} else {

			return false;

		} // if

	} // function

	public function errorInfo(){

		return $this->getObject('__db')->errorInfo();

	}

    public function errorCode(){

        return $this->getObject('__db')->errorCode();

    }

	public function query($sQuery){

		$fStartTime = microtime(true);

		$this->aObjects['__result'] = $this->getObject('__db')->query($sQuery);

		$fEndTime = microtime(true);

		$aSQLError = $this->getObject('__db')->errorInfo();

		if ($this->getProperty('yz.database.log')){

		  $this->logQuery($sQuery, $fStartTime, $fEndTime, $aSQLError);

		} // if

		$sSQLErrorClass = substr($aSQLError[0],0,2);

		if ($sSQLErrorClass == '00' || $sSQLErrorClass == '01'){

			return true;

		} else {

			return false;

		} // if

	} // function

	public function lastInsertId(){

		return $this->getObject('__db')->lastInsertId();

	} // function lastInsertId()

	// slightly different than in yzSql!
	public function escape($mString){

        if (is_array($mString)){

            return array_map(array($this,'escape'),$mString);

        } else {

            if ($this->isSetObject('__db') && method_exists($this->getObject('__db'),'escape')){

                return $this->getObject('__db')->escape($mString);

            } else {

                return addslashes(strval($mString));

            } // if

        } // if

	} // function

	// slightly different than in yzSql!
    public function quote($mString, $sQuote = null){

        if ($sQuote === null){

            $sQuote = $this->getContextProperty('quote',$this->getProperty('yz.database.engine'));

        } // if

        if (is_array($mString)){

        	return array_map(array($this,'quote'),$mString,array_fill(0,count($mString),$sQuote));

        } else {

            if ($this->isSetObject('__db') && method_exists($this->getObject('__db'),'quote')){

                return $this->getObject('__db')->quote($mString);

            } else {

                return $sQuote.(strval($mString)).$sQuote;

            } // if

        } // if

    } // function quote()

	public function getQueryLog(){

		return ($this->getObject('__log')->getNameSpace('queries'));

	} // function

	protected function logQuery($sQuery, $fStartTime, $fEndTime, $aSQLError){

		$this->getObject('__log')->addVar('queries', array('query'=>$sQuery, 'start'=>$fStartTime, 'end'=>$fEndTime, 'error_code'=>$aSQLError[0],'error_message'=>isset($aSQLError[2]) ? $aSQLError[2] : ''));

	} // function logQuery

	public function fetch($nFetchMode = null){

		if ($mFetchMode = null){

			$mFetchMode = $this->getContextProperty('fetch_mode',$this->getProperty('yz.database.driver'));

		} // if

		if ($this->isSetObject('__result')){

            return $this->getObject('__result')->fetch($mFetchMode);

        } else {

            return null;

        } // if

	} // function

	public function fetchAll($nFetchMode = null){

        if ($mFetchMode = null){

            $mFetchMode = $this->getContextProperty('fetch_mode',$this->getProperty('yz.database.driver'));

        } // if

        if ($this->isSetObject('__result')){

            return $this->getObject('__result')->fetchAll($mFetchMode);

        } else {

            return null;

        } // if

    } // function

} // class

?>